package com.rd.student.dashuju.sunwenzhuo.Test2;


public class TestBank2 implements Runnable {
    static Account2 account2;
    static double money;

    public TestBank2(Account2 account2, double money) {
        this.account2 = account2;
        this.money = money;
    }

    @Override
    public void run() {
        info2();
    }

    public static synchronized void info2() {
        if (account2.getBalance() < money) {
            System.out.println("余额不足");
            return;
        }
        System.out.println(Thread.currentThread().getName() + "可以取钱");
        account2.setBalance(account2.getBalance() - money);
        System.out.println("取钱后的余额为：" + account2.getBalance());
    }

    public static void main(String[] args) {
        Account2 account2 = new Account2("123456", 888);
        Runnable runnable1 = new TestBank2(account2, 666);
        Runnable runnable2 = new TestBank2(account2, 888);
        Thread thread1 = new Thread(runnable1,"张三");
        Thread thread2 = new Thread(runnable2,"李四");
        thread1.start();
        thread2.start();

    }
}

class Account2 {
    private String accountId;

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    private double balance;

    // 创建构造器

    public Account2(String accountId, double balance) {
        this.accountId = accountId;
        this.balance = balance;
    }
}
